<template>
  <t-dialog
    header="评奖"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="60%"
    top="5%"
  >
    <div style="height: 65vh">
      <t-form ref="form" :colon="true" :data="formData" labelWidth="100px" :rules="rules">
        <t-form-item label="成果名称">
          {{projectName}}
        </t-form-item>
        <t-form-item label="评审类型" name="reviewType">
          {{ reviewTeam.reviewType | filterByDict(dict.type.wcx_review_type) }}
        </t-form-item>
        <t-form-item label="评审平均分">
          {{reviewTeam.sumScore}}
        </t-form-item>
        <t-form-item label="年度奖励" name="year">
          <t-select @change="onYearChange" style="width: 240px" placeholder="年度奖励" :keys="{label:'year', value:'year'}"  clearable
                    :options="yearOptions" v-model="formData.year">
          </t-select>
          <t-button v-if="formData.year" @click="dialog.header='';dialog.objId=formData.annualAwardsId;dialog.open_add_edit_year_award=true"
                    variant="text" class="t-button-link">
            <t-icon name="info-circle" slot="icon"/>
            详情
          </t-button>
        </t-form-item>
        <t-form-item v-if="formData.year" label="获奖等级" name="awardsLevel">
          <t-select clearable style="width: 240px" :keys="{label: 'name', value: 'awardsLevel'}" v-model="formData.awardsLevel" @change="(val,content)=>{ formData.awardsAmount = content.option.awardsAmount;formData.awardsInfoId=content.option.id }"
                    :options="annualAwardsData.awardsInfoList"></t-select>
        </t-form-item>
        <t-form-item v-if="formData.year" label="获奖金额" name="awardsAmount">
          {{ formData.awardsAmount }}万元
        </t-form-item>
        <t-form-item label="意见" name="note">
          <t-textarea
            clearable :autosize="{minRows: 5}"
            v-model="formData.note"
            placeholder="请输入内容"
          />
        </t-form-item>
      </t-form>
    </div>
    <add-edit-year-innovation-award   :header="dialog.header" :objId="dialog.objId"  :visible.sync="dialog.open_add_edit_year_award" v-if="dialog.open_add_edit_year_award"></add-edit-year-innovation-award>
  </t-dialog>
</template>

<script>
import AddEditYearInnovationAward from "../../apply/components/add-edit-year-innovation-award.vue";
import dayjs from "dayjs"

export default {
  name: "innovation-approval",
  components: {
    AddEditYearInnovationAward
  },
  dicts: ['sys_normal_disable', 'subject_classification', 'wcx_review_type'],
  props:["objId","innovationId", "projectName", "reviewType"],
  data(){
    return {
      yearOptions: [],
      dialog: {
        open_add_edit_year_award: false
      },
      visible: true,
      loading: false,
      projectStatusOptions:[],
      reviewTeam: {
        reviewType: '',
        sumScore: 0,
      },
      formData:{
        note: '',
        reviewType: this.reviewType,
        innovationId: this.innovationId,
        awardsLevel: '',
        awardsAmount: 0,
        year: (dayjs().year() - 1).toString(),
        annualAwardsId: null,
        awardsInfoId: null,
      },
      annualAwardsData: {
        awardsInfoList: [],
      },
      rules:{
        note: [
          { required: true, message: "评奖意见不能为空"}
        ],
        year: [
          { required: true, message: "年度奖励不能为空"}
        ],
        awardsLevel: [
          { required: true, message: "获奖等级不能为空"}
        ],
      }
    }
  },
  async  created() {
    // 获取评审信息
    await this.getYearAwardList();
    this.$api.wcx.apply.innovationAwardsGet(this.innovationId).then(response=>{
      if(response.data){
        Object.assign(this.formData, response.data)
        if(!this.formData.annualAwardsId || !this.formData.year) {
          return this.getDefaultAwardsData()
        }
        this.$api.wcx.apply.annualAwardsGet(this.formData.annualAwardsId).then(response => {
          this.annualAwardsData = response.data;
        }).catch((e) => {
          this.$message.error(e.toString());
        });
      }else {
        this.getDefaultAwardsData()
      }
    }).catch((e) => {
      this.$message.error(e.toString());
    });
    this.$api.wcx.review.innovationReviewTeamGet(this.objId).then(response=>{
      if(response.data){
        Object.assign(this.reviewTeam, response.data)
        // this.formData.projectStatus = response.data.projectStatus || '1';
      }
    }).catch((e) => {
      this.$message.error(e.toString());
    });
   
  },
  methods:{
    getDefaultAwardsData(){
      this.formData.year = (dayjs().year() - 1).toString()
      const hasObj = this.yearOptions.find(item => item.year === this.formData.year)
      if(hasObj) {
        this.annualAwardsData = hasObj
      }
    },
    onYearChange(val,content){
      this.formData.awardsLevel = ""
      this.formData.annualAwardsId = content.option.id;
      this.$api.wcx.apply.annualAwardsGet(content.option.id).then(response => {
        this.annualAwardsData = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    async getYearAwardList() {
      // this.dataLoading = true;
      try {
        const res = await this.$api.wcx.apply.annualAwardsList({
          pageNum: 1,
          pageSize: 100000000
        })
        this.yearOptions = res.rows;
      }catch (e) {
        // this.dataLoading = false;
        this.$message.error(e.toString());
      }
      
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if(this.formData.id){
            this.$api.wcx.apply.innovationAwardsUpdate(this.formData).then(response => {
              this.$message.success("提交成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }else {
            this.$api.wcx.apply.innovationAwardsAdd(this.formData).then(response => {
              this.$message.success("提交成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }

        }else{
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
